﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class chonHangHoaNhaCCForm : Form
    {
        private string _maHangHoa;
        private int _hanghoaNCC_ID = -1;

        public chonHangHoaNhaCCForm(string MaHangHoa)
        {
            InitializeComponent();
            _maHangHoa = MaHangHoa;
        }

        private void chonHangHoaNhaCCForm_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            //_DB.PhieuDeNghiMuaHang_HangHoa h = dc.PhieuDeNghiMuaHang_HangHoas.Single(hh => hh.ID == _ID);
            _DB.HangHoa h = dc.HangHoas.Single(x => x.MaHangHoa == _maHangHoa);

            // show info
            txtTenHangHoa.Text = h.MaHangHoa;
            txtMaHangHoa.Text = h.TenHangHoa;
            //txtNCC.Text = h.NhaCungCap;
            //txtGia.Text = h.Gia.ToString();

            // Get the newest info from each NCC
            var nccs = from i in h.HangHoa_NhaCungCaps
                       where i.Dat == true &&
                             i.PhieuDanhGiaNCC.deleted == false &&
                             i.PhieuDanhGiaNCC.DaDuyet == true
                       group i by i.NhaCungCap into g
                       let newest = g.Max(i => i.PhieuDanhGiaNCC.NgayTao)
                       select g.Where(i => i.PhieuDanhGiaNCC.NgayTao == newest).Single();

            // Show on gridview
            dgvHangHoa.Rows.Clear();
            int count = 1;
            foreach (_DB.HangHoa_NhaCungCap n in nccs)
            {
                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvHangHoa);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = n.NhaCungCap.TenNhaCungCap;
                r.Cells[2].Value = n.ChatLuong;
                r.Cells[3].Value = n.Gia;
                r.Cells[4].Value = n.ThoiGianGiaoHang;
                r.Cells[5].Value = n.HauMai;
                r.Cells[6].Value = n.HinhThucThanhToan;
                r.Cells[7].Value = n.GhiChu;

                r.Tag = n.HangHoa_NhaCungCap_ID;

                dgvHangHoa.Rows.Add(r);
            }
        }

        private void dgvHangHoa_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dgvHangHoa.SelectedRows.Count == 1)
            {
                _hanghoaNCC_ID = (int)dgvHangHoa.SelectedRows[0].Tag;
                this.Close();
            }
        }

        public int getHangHoaNCCID()
        {
            return _hanghoaNCC_ID;
        }
    }
}
